package service

import (
	ClickHouse "gin/database"
	"gin/model"
	"gin/types"
)

type TaskWeekMeeting struct {
}

type TaskCompletionStructList struct {
	TaskId int64
}

// GetTaskCompletionCount 任务完成情况统计
func (t *TaskWeekMeeting) GetTaskCompletionCount(where interface{}, params types.GetTaskCompletionReq) (res int64, err error) {

	var modelMap = make(map[string]bool, 0)
	for _, v := range params.StepCategoryCode {
		modelMap[v] = true
	}
	if modelMap["role_model"] || modelMap["props_model"] || modelMap["scenes_model"] {
		err = ClickHouse.DB.Model(&model.Task{}).Where(where).Joins("LEFT JOIN entity on task.entity_id = entity.id").
			Select("task.id").Count(&res).Error
		return
	} else {
		err = ClickHouse.DB.Model(&model.Task{}).Where(where).
			Select("task.id").Count(&res).Error
		return
	}

}

// 任务完成情况where 条件
func (t *TaskWeekMeeting) TaskCompletionWhere(whereType string, params types.GetTaskCompletionReq, statusCodeMap map[string]*model.Status) (where interface{}) {

	var modelTag = make([]string, 0) // 角色模型,道具模型,场景模型
	var modelMap = make(map[string]bool, 0)
	// 超额时间筛选
	ExcessTimeScreening := map[string]bool{
		"ExcessFinal":         true,
		"ExcessPaid":          true,
		"Excess":              true,
		"TotalOverdueDeliver": true,
		"StillOverdue":        true,
		"TotalOverdue":        true,
	}
	for _, v := range params.StepCategoryCode {
		modelMap[v] = true
	}
	whereInterface := ClickHouse.DB.Where("JSONLength(JSONExtractString(task.json, 'task_deprecate_data')) = 0 and simpleJSONExtractInt(task.json,'task_status') <> 3")
	if _, ok := ExcessTimeScreening[whereType]; !ok {
		if params.StartTime > 0 { //计划完成时间筛选
			whereInterface.Where("task.plan_end_time >= ?", params.StartTime)
		}
		if params.EndTime > 0 {
			whereInterface.Where("task.plan_end_time <= ?", params.EndTime)
		}
	}
	if len(params.StepCategoryIds) > 0 { // 工序分类筛选
		whereInterface.Where("task.step_category_id IN ?", params.StepCategoryIds)
	}
	if len(params.ProjectIdList) > 0 {
		whereInterface.Where("task.project_id IN (?)", params.ProjectIdList)
	}
	if params.TenantId > 0 {
		whereInterface.Where("task.tenant_id = ?", params.TenantId)
	}
	// 如果包含角色模型
	if _, ok := modelMap["role_model"]; ok {
		modelTag = append(modelTag, "角色")
	}
	// 如果包含道具模型
	if _, ok := modelMap["props_model"]; ok {
		modelTag = append(modelTag, "道具")
	}
	// 如果包含场景模型
	if _, ok := modelMap["scenes_model"]; ok {
		modelTag = append(modelTag, "场景")
	}
	if len(modelTag) > 0 && modelMap["model"] {
		whereInterface.Where("JSONExtractString(entity.json, 'asset_category') IN (?)", modelTag)
	}
	// 逾期交付
	if whereType == "TotalOverdueDeliver" {
		whereInterface.
			Where("JSONExtractString(task.json, 'task_is_overdue') = 'yes'").
			Where("task.step_type = 'step_category'").
			Where("simpleJSONExtractUInt(task.json, 'task_status') = ?", statusCodeMap["final"].Id)
		//计划完成时间筛选
		if params.EndTime > 0 {
			whereInterface.Where("task.plan_end_time <= ?", params.EndTime)
		}
		where = whereInterface
	}
	// 仍逾期
	if whereType == "StillOverdue" {
		whereInterface.
			Where("JSONExtractString(task.json, 'task_is_overdue') = 'yes'").
			Where("task.step_type = 'step_category'").
			Where("simpleJSONExtractUInt(task.json, 'task_status') != ?", statusCodeMap["final"].Id)
		//计划完成时间筛选
		if params.EndTime > 0 {
			whereInterface.Where("task.plan_end_time <= ?", params.EndTime)
		}
		where = whereInterface
	}
	// 未完成
	if whereType == "undone" {
		where = whereInterface.
			Where("JSONExtractString(task.json, 'task_is_overdue') != 'yes'").
			Where("task.step_type = 'step_category'").
			Where("simpleJSONExtractUInt(task.json, 'task_status') IN (?)", params.NotStatusIds)
	}
	// 已完成
	if whereType == "final" {
		where = whereInterface.
			Where("task.step_type = 'step'").
			Where("simpleJSONExtractUInt(task.json, 'task_status') = ?", statusCodeMap["ready_to_publish"].Id)
	}
	// 已交付
	if whereType == "paid" {
		where = whereInterface.
			Where("task.step_type = 'step'").
			Where("simpleJSONExtractUInt(task.json, 'task_status') = ?", statusCodeMap["final"].Id)
	}
	// 已完成和已交付
	if whereType == "finalPaid" {
		where = whereInterface.
			Where("task.step_type = 'step'").
			Where("simpleJSONExtractUInt(task.json, 'task_status') IN (?)", []int64{statusCodeMap["final"].Id, statusCodeMap["ready_to_publish"].Id})
	}
	// 已逾期
	if whereType == "PastDue" {
		where = whereInterface.
			Where("JSONExtractString(task.json, 'task_is_overdue') = 'yes'").
			Where("task.step_type = 'step_category'").
			Where("simpleJSONExtractUInt(task.json, 'task_status') IN (?)", params.NotStatusIds)
	}
	// 总逾期
	if whereType == "TotalOverdue" {
		whereInterface.
			Where("task.step_type = 'step_category'").
			Where("JSONExtractString(task.json, 'task_is_overdue') = 'yes'")
		//计划完成时间筛选
		if params.EndTime > 0 {
			whereInterface.Where("task.plan_end_time <= ?", params.EndTime)
		}
		where = whereInterface
	}
	// 超额-已完成
	if whereType == "ExcessFinal" {
		whereInterface.
			Where("task.step_type = 'step'").
			Where("simpleJSONExtractUInt(task.json, 'task_status') = ?", statusCodeMap["ready_to_publish"].Id)
		//计划完成时间筛选
		if params.EndTime > 0 {
			whereInterface.Where("task.plan_end_time >= ?", params.EndTime)
		}
		where = whereInterface
	}
	// 超额-已交付
	if whereType == "ExcessPaid" {
		whereInterface.
			Where("task.step_type = 'step'").
			Where("simpleJSONExtractUInt(task.json, 'task_status') = ?", statusCodeMap["final"].Id)
		//计划完成时间筛选
		if params.EndTime > 0 {
			whereInterface.Where("task.plan_end_time >= ?", params.EndTime)
		}
		where = whereInterface
	}
	// 超额
	if whereType == "Excess" {
		whereInterface.
			Where("task.step_type = 'step'").
			Where("simpleJSONExtractUInt(task.json, 'task_status') IN (?)", []int64{statusCodeMap["final"].Id, statusCodeMap["ready_to_publish"].Id})
		if params.EndTime > 0 {
			whereInterface.Where("task.plan_end_time >= ?", params.EndTime)
		}
		where = whereInterface
	}
	return
}
